package com.atguigu.classroom.vod.service.impl;


import com.atguigu.classroom.model.vod.Teacher;
import com.atguigu.classroom.vo.vod.TeacherQueryVo;
import com.atguigu.classroom.vod.mapper.TeacherMapper;
import com.atguigu.classroom.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    TeacherMapper teacherMapper;
    @Override
    public List<Teacher> findAll() {
        List<Teacher> teachers = teacherMapper.selectList(null);
        return teachers;
    }

    @Override
    public boolean removeBtId(Long id) {
        int i = teacherMapper.deleteById(id);
        if(i!=0){
            return true;
        }
        return false;
    }

    @Override
    public Page<Teacher> findQueryPage(int current, int limit, TeacherQueryVo teacherQueryVo) {
        //判断条件对象teacherQueryVo是否为空；
        Page<Teacher> teacherPage=new Page<>(current,limit);
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper=new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.orderByDesc(Teacher::getUpdateTime);
        if(teacherQueryVo!=null){
            teacherLambdaQueryWrapper.like(teacherQueryVo.getName()!=null,Teacher::getName,teacherQueryVo.getName())
                    .eq(teacherQueryVo.getLevel()!=null,Teacher::getLevel,teacherQueryVo.getLevel())
                    .le(teacherQueryVo.getJoinDateEnd()!=null,Teacher::getJoinDate,teacherQueryVo.getJoinDateEnd())
                    .ge(teacherQueryVo.getJoinDateBegin()!=null,Teacher::getJoinDate,teacherQueryVo.getJoinDateBegin());
        }
        return teacherMapper.selectPage(teacherPage,teacherLambdaQueryWrapper);
    }

    @Override
    public boolean saveTeacher(Teacher teacher) {
        int i = teacherMapper.insert(teacher);
        if(i==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        int i = teacherMapper.updateById(teacher);
        if(i==0){
            return false;
        }
        return true;
    }

    @Override
    public void delBatch(List<Long> ids) {
        teacherMapper.deleteBatchIds(ids);
    }
}
